<?php
require('includes/top.php');

$empno = $_POST["empno"];
$emp_fname = $_POST["emp_fname"];
$emp_lname = $_POST["emp_lname"];
$emp_desn = $_POST["emp_desn"];
$emp_telno = $_POST["emp_telno"];
$emp_address = $_POST["emp_address"];
$emp_email = $_POST["emp_email"];
$emp_password = $_POST["empno"];
$emp_name = $_POST["empno"];
$emp_doj=$_POST["docgadate"];

$emp_accessories_name = $_POST["emp_accessories_name"];
$emp_company_name = $_POST["emp_company_name"];
$division = $_POST["division"];
$region = $_POST["region"];
$model = $_POST["model"];
$depot = $_POST["depot"];
$dispatch_location = $_POST["dispatch_location"];
//$dispatch_date = $_POST["doc3"];


//$dispatch_date = $_POST["doc3"];

$emp_oldno = $_POST["emp_oldno"];
$depot_location = $_POST["depot_location"];
$date = date("d-m-y");



//$dispatch_date = $_POST["doc3"];


//$dispatch_date = $_POST["doc3"];


/*$query = "SELECT * FROM users WHERE `user_no` = '{$empno}'";
$result = mysql_query($query);
if ( mysql_num_rows ( $result )> 0 )
{
    header("location:".$_REQUEST['PageName']."&aexit=1");
	die();
}
else
 {*/



if($emp_oldno=="") 
{ 
echo "here";


$sql="INSERT INTO users(`user_groups_id`,`user_no`,`user_firstname`,`user_lastname`,`user_telno`,`user_address`,`user_email_address`,`user_password`,`user_name`,`user_designation`,`depot`,`date`,`user_doj`) VALUES ('1','$empno','$emp_fname','$emp_lname','$emp_telno','$emp_address','$emp_email','$empno','$empno','$emp_desn','$depot','$date','$emp_doj')";
$result=mysql_query($sql);


// recipients
$to  = $emp_email; // 
// subject
$subject = 'Login Details';

// message
$message = '
<html>
<head>
  <title>Following is your Login Details:</title>
</head>
<body>
  <p><strong>Following is your Login Details:</strong></p>
  <table>
    <tr>
      <td>Username:</td><td>'.$empno.'</td>
    </tr>
    <tr>
      <td>Password:</td><td>'.$empno.'</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . $to . "\r\n";
$headers .= 'From: no-reply@laptrak.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);




if($result!=1)
	{
 	header("location:".$_REQUEST['PageName']."&aexit=1");
	die();
	}

$user_id = mysql_insert_id();

for ($i = 0; $i <count($_POST[acc_data]); $i++) {
$acc1 = $_POST[acc_data][$i];
$query1 = "Select accessoriesdata_id from accessories_data where accessories_name='".$acc1."'";
$resultquery1 = mysql_query($query1);
if($acc1=="Data card")
{
$sql8 = "Select min(datacard_id),card_no from data_card where datacard_name='".$emp_company_name."' and status=0";
$result8 = mysql_query($sql8);
	while($row8 = mysql_fetch_row($result8))
	{
	$card_no = $row8[1];
	$sql10="INSERT INTO accessories(`accessories_id`,`user_id`,`accessories_name`,`company_name`,`division`,`region`,`depot_location`,`dispatch_location`,`card_no`,`date`,`user_name`) VALUES ('".$resultquery1[0]."','$user_id','$acc1','$emp_company_name','$division','$region','$depot_location','$dispatch_location','$card_no','$date','".$_SESSION['user_name']."')";
	$result10=mysql_query($sql10);
	
	$sql9 = "Update data_card set `status`='1' where datacard_id='".$row8[0]."'";
	$result9 = mysql_query($sql9);
	
	
	}
	}
else {
	$sql1="INSERT INTO accessories(`accessories_id`,`user_id`,`accessories_name`,`company_name`,`division`,`region`,`depot_location`,`dispatch_location`,`date`,`user_name`) VALUES ('".$resultquery1[0]."','$user_id','$acc1','$emp_company_name','$division','$region','$depot_location','$dispatch_location','$date','".$_SESSION['user_name']."')";
	$result1=mysql_query($sql1);
	}
	
	$sql11 = "Select accessories_id,quantity from accessories_stock where accessories_id='".$resultquery1[0]."'";
			$result11 = mysql_query($sql11);
			$rows = mysql_fetch_array($result11);
			
			$quantity = $rows['quantity'] - 1;
			
			
			$sql10 = "Update accessories_stock set `quantity`='".$quantity."' where accessories_id='".$resultquery1[0]."'";
			$result10 = mysql_query($sql10);
}

}
else
		{
		
		$query1 = "SELECT user_id FROM users WHERE `user_no` = '{$emp_oldno}'";
		$result2 = mysql_query($query1);
		if (mysql_num_rows ( $result2)!=1)
		{
			header("location:".$_REQUEST['PageName']."&nexit=1");
			die();
		}
		else
		{
			
		$row = mysql_fetch_row($result2);
		$uno = $row[0];
		
		for ($i = 0; $i <count($_POST[acc_data]); $i++) {
		$acc1 = $_POST[acc_data][$i];		
		$query1 = mysql_query("Select accessoriesdata_id from accessories_data where accessories_name='".$acc1."'");
		$resultquery1 = mysql_fetch_array($query1);
		if($acc1=="Data card")
			{			
			$sql8 = "Select min(datacard_id),card_no from data_card where datacard_name='".$emp_company_name."' and status=0";
			$result8 = mysql_query($sql8);
			while($row8 = mysql_fetch_row($result8))
			{
			$card_no = $row8[1];
			
		$sql1="INSERT INTO accessories(`accessories_id`,`user_id`,`accessories_name`,`company_name`,`division`,`region`,`depot_location`,`dispatch_location`,`card_no`,`date`,`user_name`) VALUES ('".$resultquery1[0]."','$uno','$acc1','$emp_company_name','$division','$region','$depot_location','$dispatch_location','$card_no','$date','".$_SESSION['user_name']."')";
			$result1=mysql_query($sql1);
			
			$sql9 = "Update data_card set `status`='1' where datacard_id='".$row8[0]."'";
			$result9 = mysql_query($sql9);			
			}
			}
		else 
			{				
			$sql10="INSERT INTO accessories(`accessories_id`,`user_id`,`accessories_name`,`company_name`,`division`,`region`,`depot_location`,`dispatch_location`,`date`,`user_name`) VALUES ('".$resultquery1[0]."','$uno','$acc1','$emp_company_name','$division','$region','$depot_location','$dispatch_location','$date','".$_SESSION['user_name']."')";
			$result10=mysql_query($sql10);
			$aid = mysql_insert_id();
			} 
			
$sql11 = "Select accessories_id,quantity from accessories_stock where accessories_id='".$resultquery1[0]."'";
			$result11 = mysql_query($sql11);
			$rows = mysql_fetch_array($result11);
			
			$quantity = $rows['quantity'] - 1;
			
			
			$sql10 = "Update accessories_stock set `quantity`='".$quantity."' where accessories_id='".$resultquery1[0]."'";
			$result10 = mysql_query($sql10);			

			
$delivery_date = getDeliverydate($emp_oldno);
$rules_date =  getDaysfromAccRules($acc1);
$days = (strtotime("$delivery_date") - strtotime(date("d-m-Y"))) / (60 * 60 * 24);

if($days < $rules_date)
{
$mess = 'success';
$datamess= 'You request is before warranty';
}
else
{
$mess = '';
$datamess= '';
}

$sql11 = "Update accessories set `message`='$datamess' where accessories_id='".$aid."'";
$result11 = mysql_query($sql11);
				
		
		}
		}
	
}
//}

//echo $mess;
//exit;


		header("location:".$_REQUEST['PageName']."&v=success".'&mess=' . $mess ); 

?>
